package com.itheima.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectinDemo01 {
    public static void main(String[] args) {
        Collection<String> list = new ArrayList<>();
        String s1 = "JavaEE企业级开发指南";
        String s2 = "Oracle高级编程";
        String s3 = "MySQL从入门到精通";
        String s4 = "Java架构师之路";
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);

        printList(list);
        System.out.println("----------");
        printListLow10(list);
        System.out.println("----------");
        printListChar(list);
        System.out.println("----------");
        printListRemove(list);
        System.out.println("----------");
    }

    //如果书名中包含“Oracle”，则删掉此书。删掉后，遍历集合，打印所有书名。
    public static void printListRemove(Collection<String> list) {
        Iterator<String> iterator = list.iterator();
        System.out.println("不包含Oracle的书如下：");
        while (iterator.hasNext()) {
            String s = iterator.next();
            if (s.contains("Oracle")) {
                iterator.remove();
            } else {
                System.out.println(s);
            }
        }
    }

    //使用迭代器遍历所有元素，筛选书名中包含“Java”的，并打印
    public static void printListChar(Collection<String> list) {
        System.out.println("包含Java的书如下：");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String s = iterator.next();
            if (s.contains("Java")) {
                System.out.println(s);
            }

        }
    }

    //使用迭代器遍历所有元素，筛选书名小于10个字符的，并打印；
    public static void printListLow10(Collection<String> list) {
        Iterator<String> iterator = list.iterator();
        System.out.println("书名小于10个字符的如下：");
        while (iterator.hasNext()) {
            String s = iterator.next();
            if (s.length() < 10) {
                System.out.println(s);
            }
        }
    }

    //使用迭代器遍历所有元素，并打印
    public static void printList(Collection<String> list) {
        Iterator<String> iterator = list.iterator();
        System.out.println("所有书如下：");
        while (iterator.hasNext()) {
            String s = iterator.next();
            System.out.println(s);
        }
    }
}
